package com.athena.util;

import com.athena.config.DefaultAppConfig;
import com.athena.framework.annotation.Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis连接工具
 *
 * @author likun
 * 2018/6/26 16:33
 **/
@Bean
public class RedisClient {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(RedisClient.class);

    private static JedisPool pool;

    public RedisClient() {
        if (pool == null) {
            logger.info("连接redis:{}", DefaultAppConfig.redisUrl);
            JedisPoolConfig config = createConfig();
            /*pool = new JedisPool(config,
                    DefaultAppConfig.redisUrl,
                    DefaultAppConfig.redisPort,
                    10000,
                    DefaultAppConfig.redisPassword);*/

            pool = new JedisPool(config,
                    DefaultAppConfig.redisUrl,
                    DefaultAppConfig.redisPort,
                    10000,
                    DefaultAppConfig.redisPassword);
            logger.info("连接redis成功.");
        }
    }

    private JedisPoolConfig createConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        //连接池最大空闲连接数
        config.setMaxIdle(10);
        //最大连接数
        config.setMaxTotal(1000);
        //超时时间
        config.setMaxWaitMillis(10000);
        //使用连接时，测试连接是否可用
        config.setTestOnBorrow(true);
        //返回连接时，测试连接是否可用
        config.setTestOnReturn(true);
        return config;
    }

    /**
     * 获取redis连接
     *
     * @return
     */
    public static Jedis getJedis() {
        try {
            if (pool != null) {
                return pool.getResource();
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException("获取redis连接失败!!!");
        }
    }

    /**
     * 关闭连接
     * @param jedis
     */
    public static void returnJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * 设置值
     *
     * @param key
     * @param value
     */
    public void setValue(String key, String value) {
        Jedis jedis = getJedis();
        jedis.set(key, value);
        returnJedis(jedis);
    }

    public void setValue(byte[] key, byte[] data) {
        Jedis jedis = getJedis();
        jedis.set(key, data);
        returnJedis(jedis);
    }

    /**
     * 获取值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        Jedis jedis = getJedis();
        String value = jedis.get(key);
        returnJedis(jedis);
        return value;
    }

    public byte[] getValue(byte[] key) {
        Jedis jedis = getJedis();
        byte[] bytes = jedis.get(key);
        returnJedis(jedis);
        return bytes;
    }


    /**
     * 删除
     *
     * @param key
     */
    public void delete(String key) {
        Jedis jedis = getJedis();
        jedis.del(key);
        returnJedis(jedis);
    }

    public void delete(byte[] key) {
        Jedis jedis = getJedis();
        jedis.del(key);
        returnJedis(jedis);
    }

}
